<?php
namespace admin\action;

use herosphp\http\HttpRequest;
use herosphp\utils\AjaxResult;
use herosphp\bean\Beans;

/**
 *
 **/
class NavAction extends CommonAction {

	public function C_start()
	{
		parent::C_start();
	}
	protected $serviceBean="admin.adminNav.service";

	public function add(HttpRequest $request) {
	
		parent::add($request);
		$this->assign("title", "添加导航");
		$this->assign('bread', array('网站设置','导航设置',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;',url('admin/nav/index'),'javascript:;'));
	
	}
	
	/**-- actionEdit --**/
	public function edit(HttpRequest $request) {
		$id = $request->getParameter('id');
		if ( $id == '' ) {
			$this->showMessage('danger', COM_ERR_MSG);
		} else {
			$conditions = $this -> getTemplateVar('conditions');
			$conditions['id'] = $id;
			$service = Beans::get($this->getServiceBean());
			$item = $service->getItem($conditions);
			if($item['is_default']==1)
			{
				location(url('admin/nav/index'));
			}
			$this->assign('id',$id);
			$this->assign('item', $item);
		}

		$this->assign("title", "编辑导航");
		$this->assign('bread', array('网站设置','导航设置',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;',url('admin/nav/index'),'javascript:;'));
		$this->setView(strtolower($request->getAction()).'/add');
	}
	
	/**-- actionIndex --**/
	public function index(HttpRequest $request) {
		$conditions = array();
		$this->assign('_table','导航设置');
		$s_name = $request->getParameter('s_name');
		$this->assign('s_name',$s_name);
		if($s_name !== '' && $s_name!== NULL)$conditions['name'] = '%'.$s_name.'%';
		$s_url = $request->getParameter('s_url');
		$this->assign('s_url',$s_url);
		if($s_url !== '' && $s_url!== NULL)$conditions['url'] = '%'.$s_url.'%';
		$s_addtime = $request->getParameter('s_addtime');
		$this->assign('s_addtime',$s_addtime);
		if($s_addtime !== '' && $s_addtime!== NULL)$conditions['addtime'] = '%'.$s_addtime.'%';
		$s_edittime = $request->getParameter('s_edittime');
		$this->assign('s_edittime',$s_edittime);
		if($s_edittime !== '' && $s_edittime!== NULL)$conditions['edittime'] = '%'.$s_edittime.'%';
		$s_display = $request->getParameter('s_display');
		$this->assign('s_display',$s_display);
		if($s_display !== '' && $s_display!== NULL)$conditions['display'] = '%'.$s_display.'%';
		$s_blank = $request->getParameter('s_blank');
		$this->assign('s_blank',$s_blank);
		if($s_blank !== '' && $s_blank!== NULL)$conditions['blank'] = '%'.$s_blank.'%';
		$s_manager_id = $request->getParameter('s_manager_id');
		$this->assign('s_manager_id',$s_manager_id);
		if($s_manager_id !== '' && $s_manager_id!== NULL)$conditions['manager_id'] = $s_manager_id;
		$s_app_id = $request->getParameter('s_app_id');
		$this->assign('s_app_id',$s_app_id);
		if($s_app_id !== '' && $s_app_id!== NULL)$conditions['app_id'] = '%'.$s_app_id.'%';
		$this->assign('_filter',array('name','url','addtime','edittime','manager_id','app_id'));
		$this->assign('_header',array('导航名称','链接地址','添加时间','修改时间','添加人','appid'));
		$this->setConditions($conditions);
	
		$pagesize = $request->getParameter('pagesize') ? $request->getParameter('pagesize') : $this->getPagesize();
		$order = $request->getParameter('order') ? $request->getParameter('order') : $this->getOrder();
		$order = str_replace('+' , " ",$order);
		$this->setPagesize($pagesize);
		$this->setOrder($order);
		$this->assign('order',$order);
		$this->assign('pagesize',$pagesize);


		$this->page = $request->getParameter('page', 'intval');
		if ( $this->page <=0 ) {
			$this->page = 1;
		}
		$pagesize = $request->getParameter('pagesize') ? $request->getParameter('pagesize') : $this->getPagesize();
		$order = $request->getParameter('order') ? $request->getParameter('order') : $this->getOrder();
		$order = str_replace('+' , " ",$order);
		$this->setPagesize($pagesize);
		$this->setOrder($order);
		$this->assign('order',$order);
		$this->assign('pagesize',$pagesize);
		//parent::index($request);
		$service = Beans::get($this->getServiceBean());
		$total = $service->count($this->getConditions());
		$items = $service->getItems($this->getConditions(), $this->getFields(), $this->getOrder(),
				array($this->getPage(), $this->getPagesize()), $this->getGroup(), $this->getHaving());
		$this->assign('items', $items);

		$this->generatePageMenu($total);
		$this->assign("title", "导航设置");
		$this->assign('bread', array('网站设置',$this->getTemplateVar('title')));
		$this->assign('breadUrl', array('javascript:;','javascript:;'));
		$this->setView(strtolower($request->getAction()) . '/index');
	}
	
	/**-- doAddMethod --**/
	public function do_add(HttpRequest $request)
	{
		$data = $request->getParameter('data');
		if(!$data['display']){$data['display'] = 0;}
		else{$data['display'] = 1;}
		if(!$data['blank']){$data['blank'] = 0;}
		else{$data['blank'] = 1;}
	
		$request->addParameter('data',$data);
		parent::do_add($request);	
	}
	
	/**-- doEditMethod --**/
	public function do_edit(HttpRequest $request){
		$data = $request->getParameter('data');
		$id = $request->getParameter('id');

		if ( !$data ) AjaxResult::ajaxFailtureResult();

		if ( $id == "" ) AjaxResult::ajaxResult('error', COM_ERR_MSG);

		$navService=Beans::get($this->getServiceBean());
		$item = $navService->getItem(array('id'=>$id));

		//系统默认导航栏，不可编辑
		if($item['is_default']==1) {
			//location(url('admin/nav/index'));
            if($item['name'] != '行业资讯'){
                AjaxResult::ajaxResult(AjaxResult::OP_FAILURE,'系统导航，不允许编辑');
            }
        }

		if(!$data['display']){$data['display'] = 0;}
		else{$data['display'] = 1;}
		if(!$data['blank']){$data['blank'] = 0;}
		else{$data['blank'] = 1;}


		if ( $navService->update($data, $id) ) {
			AjaxResult::ajaxSuccessResult();
		} else {
			AjaxResult::ajaxFailtureResult();
		}
	}

	public function 的delete(HttpRequest $request)
	{
		$id = $request->getParameter('id');
		$navService=Beans::get($this->getServiceBean());
		$item = $navService->getItem(array('id'=>$id));
		//系统默认导航栏，不可删除
		if($item['is_default']==1)
		{
			AjaxResult::ajaxFailtureResult();
		}
		parent::delete();
	}

}
